package com.itsm.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itsm.entity.ObjectRESTResult;
import com.itsm.utils.UploadUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Controller
public class ImageUploadController {
    @ResponseBody
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile imgFile, HttpServletRequest request, HttpSession session){

        ObjectRESTResult restResult = new ObjectRESTResult();

        // 生成唯一文件名
//        String uuid = UUID.randomUUID().toString().trim();
        String fileNames = imgFile.getOriginalFilename();

        // 调用UploadUtils工具类将图片存放到服务器上
        File fileDir = UploadUtils.getImgDirFile();

        try {
            // 构建真实的文件路径
            File newFile = new File(fileDir.getAbsolutePath() + File.separator + fileNames);
            imgFile.transferTo(newFile);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> emp = new HashMap<>();
        emp.put("fileName",fileNames);
        //返回图片名称
        restResult.setReturnObj(emp);
        return null;
    }
    @ResponseBody
    @RequestMapping("/upload1")
    public String upload1(@RequestParam("file") List<MultipartFile> imgFile, HttpServletRequest request, HttpSession session){

        ObjectRESTResult restResult = new ObjectRESTResult();
        Map<String,Object> emp = new HashMap<>();
        for(MultipartFile file:imgFile){
            String fileNames = file.getOriginalFilename();

            // 调用UploadUtils工具类将图片存放到服务器上
            File fileDir = UploadUtils.getImgDirFile();

            try {
                // 构建真实的文件路径
                File newFile = new File(fileDir.getAbsolutePath() + File.separator + fileNames);
                file.transferTo(newFile);

            } catch (IOException e) {
                e.printStackTrace();
            }

            emp.put("fileName",fileNames);
        }
        // 生成唯一文件名
//        String uuid = UUID.randomUUID().toString().trim();
        //返回图片名称
        restResult.setReturnObj(emp);
        return null;
    }




}
